package com.yindian.cos.easy.security.filter;

import com.yindian.cos.easy.security.authentication.AuthenticationChain;
import com.yindian.cos.easy.security.handler.AuthenticationFailureHandler;
import com.yindian.cos.easy.security.handler.AuthenticationSuccessHandler;
import com.yindian.cos.easy.security.handler.LoginSuccessHandler;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器入口类，负责拦截器链的调用
 *
 * @author zhangchuanhao
 * @create 2018/11/18
 */
@Log4j2
public class DispatchFilter extends OncePerRequestFilter {

    private final DispatchFilterHelper dispatchFilterHelper;

    public DispatchFilter(AuthenticationChain authenticationChain,
                          AuthenticationFailureHandler authenticationFailureHandler,
                          LoginSuccessHandler loginSuccessHandler,
                          AuthenticationSuccessHandler authenticationSuccessHandler) {
        this.dispatchFilterHelper = new DispatchFilterHelper(authenticationChain, authenticationFailureHandler, loginSuccessHandler, authenticationSuccessHandler);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        log.debug("进入DispatchFilter，method = {}，url = {}", request.getMethod(), request.getRequestURL());
        dispatchFilterHelper.dispatch(request, response, filterChain);
    }
}
